/**********************************************************************
 * Copyright (c) 2008, j. montgomery                                  *
 * All rights reserved.                                               *
 *                                                                    *
 * Redistribution and use in source and binary forms, with or without *
 * modification, are permitted provided that the following conditions *
 * are met:                                                           *
 *                                                                    *
 * + Redistributions of source code must retain the above copyright   *
 *   notice, this list of conditions and the following disclaimer.    *
 *                                                                    *
 * + Redistributions in binary form must reproduce the above copyright*
 *   notice, this list of conditions and the following disclaimer     *
 *   in the documentation and/or other materials provided with the    *
 *   distribution.                                                    *
 *                                                                    *
 * + Neither the name of j. montgomery's employer nor the names of    *
 *   its contributors may be used to endorse or promote products      *
 *   derived from this software without specific prior written        *
 *   permission.                                                      *
 *                                                                    *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS*
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  *
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS  *
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE     *
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,*
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES           *
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,*
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)      *
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED*
 * OF THE POSSIBILITY OF SUCH DAMAGE.                                 *
 **********************************************************************/
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace UpdateDnsOMatic.Configuration
{
    internal class ProxySettings
    {
        internal static bool IsEnabled
        {
            get
            {
                if (ConfigurationManager.AppSettings["Proxy_IsEnabled"] == null)
                    return false; // Default to disabled
                else
                    return bool.Parse(ConfigurationManager.AppSettings["Proxy_IsEnabled"]);
            }
        }

        internal static bool UseDefault
        {
            get
            {
                if (ConfigurationManager.AppSettings["Proxy_UseDefault"] == null)
                    return true; // Default to use the default system web proxy
                else
                    return bool.Parse(ConfigurationManager.AppSettings["Proxy_UseDefault"]);
            }
        }

        internal static bool UseDefaultCredentials
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if ((ConfigurationManager.AppSettings["Proxy_UseDefaultCredentials"] == null) ||
                     (ConfigurationManager.AppSettings["Proxy_UseDefaultCredentials"].Equals(string.Empty)))
                    return true;  // Default is to use Default Credentials
                else
                    return bool.Parse(ConfigurationManager.AppSettings["Proxy_UseDefaultCredentials"]);
            }
        }

        internal static string Server
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if (ConfigurationManager.AppSettings["Proxy_Server"] == null)
                    return string.Empty; // Default is none
                else 
                    return ConfigurationManager.AppSettings["Proxy_Server"];
            }
        }

        internal static int BasicPort
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if ((ConfigurationManager.AppSettings["Proxy_BasicPort"] == null) ||
                     (ConfigurationManager.AppSettings["Proxy_BasicPort"].Equals(string.Empty)))
                    return 8080; // Default Port
                else 
                    return int.Parse(ConfigurationManager.AppSettings["Proxy_BasicPort"]);
            }
        }

        internal static int TlsPort
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if ((ConfigurationManager.AppSettings["Proxy_TlsPort"] == null) ||
                      (ConfigurationManager.AppSettings["Proxy_TlsPort"].Equals(string.Empty)))
                    return 8443; // Default Port
                else
                    return int.Parse(ConfigurationManager.AppSettings["Proxy_TlsPort"]);
            }
        }

        internal static string UserName
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if ((ConfigurationManager.AppSettings["Proxy_UserName"] == null) ||
                     (ConfigurationManager.AppSettings["Proxy_UserName"].Equals(string.Empty)))
                    return string.Empty;  // Default is none
                else
                return ConfigurationManager.AppSettings["Proxy_UserName"];
            }
        }

        internal static string Password
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if ((ConfigurationManager.AppSettings["Proxy_Password"] == null) ||
                   (ConfigurationManager.AppSettings["Proxy_Password"].Equals(string.Empty)))
                    return string.Empty;  // Default is none
                else
                return ConfigurationManager.AppSettings["Proxy_Password"];
            }
        }

        internal static string Domain
        {
            get
            {
                if (!IsEnabled)
                    throw new InvalidOperationException("The use of the proxy server is disabled. Update the configuration file to enable the proxy server.");

                if ((ConfigurationManager.AppSettings["Proxy_Domain"] == null) ||
                      (ConfigurationManager.AppSettings["Proxy_Domain"].Equals(string.Empty)))
                    return string.Empty;  // Default is none
                else
                    return ConfigurationManager.AppSettings["Proxy_Domain"];
            }
        }

        internal static IWebProxy GetProxy(bool isTlsEnabled)
        {
            IWebProxy currentProxy = new WebProxy();
            // Use a proxy?
            if (ProxySettings.IsEnabled)
            {
                // Setup Proxy
                if (ProxySettings.UseDefault)
                {
                    currentProxy = WebRequest.GetSystemWebProxy(); // get's the proxy for the impersonated calling user
                }
                else
                {
                    // Yes, Determine if we are use TLS/SSL or Basic Proxy port (8080 vs. 8443)
                    int proxyPort = isTlsEnabled ? ProxySettings.TlsPort : ProxySettings.BasicPort;
                    currentProxy = new WebProxy(ProxySettings.Server, proxyPort);

                }

                // Setup Credentials
                if (ProxySettings.UseDefaultCredentials)
                {
                    // Use Default Credentials
                    currentProxy.Credentials = CredentialCache.DefaultCredentials;
                }
                else
                {
                    // Use Credentials specified in the config file
                    if (ProxySettings.Domain.Equals(string.Empty))
                        currentProxy.Credentials = new NetworkCredential(ProxySettings.UserName, ProxySettings.Password);
                    else
                        currentProxy.Credentials = new NetworkCredential(ProxySettings.UserName, ProxySettings.Password, ProxySettings.Domain);
                }
            }
            else
            {
                // No Proxy settings, fall back to default
                currentProxy = WebRequest.DefaultWebProxy;
            }

            return currentProxy;
        }
    }
}
